using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Xml;


public partial class Inbox : System.Web.UI.Page
{
    private ArrayList tableHeadersList = new ArrayList();
    private ArrayList tableColumnsList = new ArrayList();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            
        }
        String[] headerlist = { "TaskId", "Status", "Activity", "Process Name", "Assignee", "Received Date", "Start Date", "Due Date" };
        tableHeadersList.AddRange(headerlist);

        String[] columnlist = { "TaskId", "State", "Activity", "ProcessName", "Assignee", "DeliveryDate", "StartDate", "DueDate" };
        tableColumnsList.AddRange(columnlist);

        createTableHeaders();
        getTasks();
    }

    private void createTableHeaders()
    {
        TableHeaderRow headerRow = new TableHeaderRow();
        TableHeaderCell cell = null;

        foreach (String headername in tableHeadersList)
        {
            cell = new TableHeaderCell();
            cell.Text = headername;
            cell.BorderWidth = Unit.Pixel(1);
            headerRow.Cells.Add(cell);    
        }

        Table1.Rows.Add(headerRow);
    }

    private void getTasks()
    {
        String getTasksResponse = Util.run(RequestXML.getTasks);

        XmlDocument xmldoc = new XmlDocument();
        xmldoc.LoadXml(getTasksResponse);
        XmlNodeList tasks = xmldoc.SelectNodes("//*[local-name()='Task']");

        foreach (XmlNode tasknode in tasks)
        {
            createRow(tasknode);
        }
    }

    private void createRow(XmlNode tasknode)
    {
        TableRow row = new TableRow();
        String taskid = String.Empty;
        String targetdn = String.Empty;

        TableCell cell = null;
        foreach (String tagname in tableColumnsList)
        {
            String data = tasknode.SelectSingleNode(".//*[local-name()='"+tagname+"']").InnerText;
            if (tagname.CompareTo("TaskId")==0)
            {
                taskid = data;
            }
            if (tagname.CompareTo("Assignee")==0)
            {
                targetdn = data;
            }
            cell = new TableCell();
            cell.Text = data;
            cell.BorderWidth = Unit.Pixel(1);
            row.Cells.Add(cell);
        }

        row.Attributes.Add("onClick", "javascript:window.open('taskdetails.aspx?taskid="+taskid+"&targetdn="+targetdn+"')");
        row.Attributes.Add("onmouseOver", "javascript:this.style.cursor='hand'");
        Table1.Rows.Add(row);
    }
}
